package com.demoworld.dao;

import java.io.IOException;
import java.sql.*;

/**
 * Created by francis on 2018/1/20.
 */
public class DBHelper {

    public static final String url = "jdbc:mysql://localhost:3306/test";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "123456";
    public Connection conn = null;
    public PreparedStatement pst = null;
    public Statement statement;

    public void excuteSelect(String sql) throws IOException {
        try {
            Class.forName(name);//指定连接类型
            conn = DriverManager.getConnection(url, user, password);//获取连接
            pst = conn.prepareStatement(sql);//准备执行语句
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void excuteUpdate() throws IOException {
        try {
            Class.forName(name);//指定连接类型
            conn = DriverManager.getConnection(url, user, password);//获取连接
            statement = conn.createStatement();//准备执行语句
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            this.conn.close();
            this.pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
